package schoolsys.controller;
import schoolsys.entity.Student;
import schoolsys.service.StudentService;
import schoolsys.service.impl.StudentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 */ // 查找所有数据
@WebServlet(name = "SelectStuListServlet", value = "/SelectStuListServlet")
/**
 * author:wxy
 * create_Date:2021/10/19 11:05
 * 查询学生servlet
 * tb_name:student
 */
public class SelectStuListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取数据库数据

        StudentService studentService = new StudentServiceImpl();
        String studentName = request.getParameter("stuName");
        String studentNo = request.getParameter("stuNo");

        //
        String currentPage=request.getParameter("currentPage");
        int pageNo=1; //默认为1
        if(currentPage!=null&&!"".equals(currentPage)){
            pageNo= Integer.parseInt(currentPage);
        }

        List<Student> stuByNameAll = studentService.getStuByNameAll(studentName,studentNo,pageNo,3);
        //重新绑定查询数据到页面
        request.setAttribute("stuName",studentName);
        request.setAttribute("stuNo",studentNo);
        //分页，需要返回currentPage，totalPage，stuList
        request.setAttribute("currentPage",pageNo);
        int totalCount=studentService.getTotalCount(studentName,studentNo);
        int totalPage=0;
        if(totalCount%3==0){
            totalPage=totalCount/3;
        }else{
            totalPage=totalCount/3+1;
        }
        request.setAttribute("totalPage",totalPage);
        request.setAttribute("stuByNameAll",stuByNameAll);
        String action = request.getParameter("action");
        if ("delete".equals(action)){
            String n= request.getParameter("n");
            request.setAttribute("n",n);
            request.getRequestDispatcher("/myschool/stuList.jsp").forward(request,response);

        }else if ("update".equals(action)){
            request.setAttribute("student",stuByNameAll.get(0));
            request.getRequestDispatcher("/myschool/updatestu.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("/myschool/stuList.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
